﻿using YouWei.Winform.Common;
using RestSharp.Contrib;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;

namespace YouWei.Winform
{
    public class RequestHelper
    {
        public static ApiResult PostHtml(string url, string paramData, Encoding pEncoding, string userAgent, CookieContainer ck)
        {
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            HttpWebResponse objResponse = null;

            ApiResult rt = ApiResult.Success("");

            if (request != null)
            {
                request.Accept = "application/json, text/javascript, */*; q=0.01";
                request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.9");
                request.Headers.Add("X-Requested-With", "XMLHttpRequest");
                request.Referer = "https://" + request.RequestUri.Host + "/views/inquiry.html?q=j";

                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
                request.CookieContainer = ck;
                request.Date = DateTime.Now;
                request.UserAgent = userAgent;

                string data = paramData;
                byte[] byteArray = pEncoding.GetBytes(data);
                request.ContentLength = byteArray.Length;
                Stream stream = request.GetRequestStream();
                stream.Write(byteArray, 0, byteArray.Length);
                stream.Close();


                request.MaximumAutomaticRedirections = int.MaxValue;
                //允许请求重定向  默认值为false
                request.AllowAutoRedirect = true;
                request.Timeout = 300000;
                request.IfModifiedSince = DateTime.Now;
                try
                {

                    objResponse = request.GetResponse() as HttpWebResponse;
                    if (objResponse != null && objResponse.StatusCode == HttpStatusCode.OK)
                    {
                        Stream objStream = objResponse.GetResponseStream();
                        if (objStream != null)
                        {
                            StreamReader objStreamReader = new StreamReader(objStream, pEncoding);
                            objStream.ReadTimeout = 15000;
                            rt.message = objStreamReader.ReadToEnd();
                            objStreamReader.Close();
                            objStreamReader.Dispose();
                        }
                    }
                    else
                    {
                        rt.message = "获取综合应用大厅接口服务出错";
                        rt.status = -1;
                    }
                }

                catch (Exception ex)
                {
                    rt.message = ex.Message;
                    rt.status = -1;
                }
                finally
                {
                    objResponse?.Close();
                    request.Abort();
                }

            };
            return rt;
        }

        public static ApiFileResult GetFile(string url, string userAgent, CookieContainer ck)
        {
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            HttpWebResponse objResponse = null;

            ApiFileResult rt = new ApiFileResult()
            {
                message = "ok",
                status = 1
            };

            if (request != null)
            {
                request.Accept = "image/webp,image/apng,image/*,*/*;q=0.8";
                request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.9");
                request.Referer = "https://" + request.RequestUri.Host;
                request.Host = request.RequestUri.Host;

                request.Method = "GET";
                //request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
                request.CookieContainer = ck;
                request.Date = DateTime.Now;
                request.UserAgent = userAgent;

                request.MaximumAutomaticRedirections = int.MaxValue;
                //允许请求重定向  默认值为false
                request.AllowAutoRedirect = true;
                request.Timeout = 300000;
                request.IfModifiedSince = DateTime.Now;
                try
                {

                    objResponse = request.GetResponse() as HttpWebResponse;
                    if (objResponse != null && objResponse.StatusCode == HttpStatusCode.OK)
                    {
                        Stream stream = objResponse.GetResponseStream();
                        if (stream != null)
                        {
                            byte[] array = new byte[16384];
                            using (MemoryStream memoryStream = new MemoryStream())
                            {
                                int count;
                                while ((count = stream.Read(array, 0, array.Length)) > 0)
                                {
                                    memoryStream.Write(array, 0, count);
                                }
                                rt.bytes= memoryStream.ToArray();
                            }

                        }
                        else
                        {
                            rt.message = objResponse.StatusDescription;
                            rt.status = -1;
                        }
                        stream.Close();
                    }
                    else
                    {
                        rt.message = objResponse.StatusDescription;
                        rt.status = -1;
                    }
                }

                catch (Exception ex)
                {
                    rt.message = ex.Message;
                    rt.status = -1;
                }
                finally
                {
                    objResponse?.Close();
                    request.Abort();
                }

            };
            return rt;
        }
    }
}
